package com.car.car.controller;


import com.car.car.VO.ProductInfoVO;
import com.car.car.VO.ProductVO;
import com.car.car.VO.ResultVO;
import com.car.car.dataobject.ProductCategory;
import com.car.car.dataobject.ProductInfo;
import com.car.car.service.CategoryService;
import com.car.car.service.ProductService;
import com.car.car.utils.ResultUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/buyer/product")
public class BuyerProductController {


    @Autowired
    private ProductService productService;

    @Autowired
    private CategoryService categoryService;

    @RequestMapping("/list")
    public ResultVO List(){

        List<ProductInfo> productInfoList = productService.findUpAll();

        List<Integer> categoryList = new ArrayList<>();

        for (ProductInfo productInfo : productInfoList){
            categoryList.add(productInfo.getCategoryType());
        }

        List<ProductCategory> productCategoryList = categoryService.findByCategoryTypeIn(categoryList);


        List<ProductVO> productVOList = new ArrayList<>();
        for (ProductCategory productCategory : productCategoryList){
            ProductVO productVO = new ProductVO();
            productVO.setCategoryName(productCategory.getCategoryName());
            productVO.setCategoryType(productCategory.getCategoryType());


                List<ProductInfoVO> productInfoVOList = new ArrayList<>();
                for(ProductInfo productInfo : productInfoList){
                    if(productInfo.getCategoryType().equals(productCategory.getCategoryType())){
                        ProductInfoVO productInfoVO = new ProductInfoVO();
                        BeanUtils.copyProperties(productInfo,productInfoVO);
                        productInfoVOList.add(productInfoVO);
                    }
                }
            productVO.setProductInfoVOSList(productInfoVOList);
            productVOList.add(productVO);
        }


        return ResultUtil.success(productVOList);
    }

}
